import { DirectionCode, KeyEventParameters, SceneUtils, TButton, TLayout } from "../SceneControl";
import MainPageScene from "./MainPageScene";
const { ccclass, property } = cc._decorator;

@ccclass
export default class StorePanelScene extends MainPageScene {
    content1: cc.Node;
    content2: cc.Node;
    content3: cc.Node;
    refreshNode: cc.Node;


    waitfresh: any;
    upLayout: TLayout;

    btnFree1: cc.Node;
    btnFree2: cc.Node;

    protected onEnable(): void {
        this.content1 = cc.find("scrollView/view/content/type1/New Node/content", this.node);
        this.content2 = cc.find("scrollView/view/content/type2/New Node/content", this.node);
        this.content3 = cc.find("scrollView/view/content/type3/New Node/content", this.node);
        this.refreshNode = cc.find("scrollView/view/content/type1/New Node/time/btnRefresh", this.node);


        this.btnFree1 = this.content2.children[0].getChildByName("btnFree")
        this.btnFree2 = this.content2.children[1].getChildByName("btnFree")

        super.onEnable();

        this.content1.on(cc.Node.EventType.CHILD_ADDED, this.refresh, this);
        this.btnFree1.on("active-in-hierarchy-changed", this.refresh, this);
        this.btnFree2.on("active-in-hierarchy-changed", this.refresh, this);
    }

    protected onDisable(): void {
        super.onDisable();
        this.content1.off(cc.Node.EventType.CHILD_ADDED, this.refresh, this);
        this.content2.off(cc.Node.EventType.CHILD_ADDED, this.refresh, this);
        this.btnFree1.off("active-in-hierarchy-changed", this.refresh, this);
        this.btnFree2.off("active-in-hierarchy-changed", this.refresh, this);
    }

    getUpLayout(): TLayout {
        this.upLayout = new TLayout(this.node.name);
        let btns = this.btns;
        this.upLayout.buttonList = btns;
        this.upLayout.site.row = btns.length - 1;
        return this.upLayout;
    }

    refresh() {
        if (this.waitfresh)
            return;
        this.waitfresh = true;
        this.scheduleOnce(() => {
            this.waitfresh = false;
            this.upLayout.buttonList = this.btns
            this.tscene.apply();
        }, 0.1)
    }

    get btns() {
        let nodes: TButton[][] = [[], [], []];
        let result: TButton[][] = [];

        nodes[0].push(new TButton(this.refreshNode));

        this.content1.children.forEach(item => item.getComponent(cc.Button) && nodes[1].push(new TButton(item)));
        this.content2.children.forEach(item => item.getComponentsInChildren(cc.Button).forEach(but => but.node.activeInHierarchy && nodes[2].push(new TButton(but.node))));
        this.content3.children.forEach(item => item.getComponent(cc.Button) && nodes[2].push(new TButton(item)));
        nodes.forEach(arr => {
            if (arr.length > 0) {
                result.push(arr);
            }
        })
        return result;
    }


}
